package gui;


import java.awt.Graphics;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JPanel;

public class MapComponent extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3659127287986211904L;
	private ArrayList<Rectangle2D> buildingList;
	private ArrayList<Line2D> roadList;
	private MainPanel panel;
	public MapComponent(){
		super();
	}
	public MapComponent( ArrayList<Rectangle2D> buildingList, ArrayList<Line2D> roadList, MainPanel panel){
		super();
		this.buildingList = buildingList;
		this.roadList = roadList;
		this.panel = panel;
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		int xView,yView,wView,hView;
		for (Rectangle2D building : buildingList) {
//			System.out.println("Position du Building:("+ building.getX()+","+building.getY()+")");
			xView = (int) (building.getX() * this.getWidth());
			yView =(int) (building.getY() * this.getHeight());
			wView = (int) (building.getWidth() * this.getWidth());
			hView =(int) (building.getHeight() * this.getHeight());
			
			g.drawRect(xView, yView, wView, hView);
			
//			System.out.println("Dimensions de la MAP: Hauteur: "+this.getHeight()+ ", Largeur: "+this.getWidth());
		}
		int x1View,y1View,x2View,y2View;
		for (Line2D road : roadList) {
//			System.out.println("Position du Building:("+ building.getX()+","+building.getY()+")");
			
			x1View = (int) (road.getX1() * this.getWidth());
			y1View =(int) (road.getY1() * this.getHeight());
			x2View = (int) (road.getX2() * this.getWidth());
			y2View =(int) (road.getY2() * this.getHeight());
			
			g.drawLine(x1View, y1View, x2View, y2View);			
//			System.out.println("Dimensions de la MAP: Hauteur: "+this.getHeight()+ ", Largeur: "+this.getWidth());
		}
		
	}

	public void addBuilding(Rectangle2D building) {
		buildingList.add(building);
		this.panel.addBuildPop();
		repaint();
	}
	
	public void modifyBuildingPosition(Rectangle2D building, double x, double y){
		buildingList.get(buildingList.indexOf(building)).setRect(x, y, building.getWidth(), building.getHeight());
		repaint();
	}

	public void removeBuilding(Rectangle2D building) {
//		System.out.println("remove building: "+building.toString());
		buildingList.remove(building);
		this.panel.killBuild();
		repaint();
	}

	
	public void addRoad(Line2D road) {
		roadList.add(road);
		repaint();
	}
	
	// useless?? Road haven't got to change their positions
	public void modifyRoadPosition(Line2D road, double x1, double y1, double x2, double y2){
		roadList.get(roadList.indexOf(road)).setLine(x1, y1, x2, y2);
		repaint();
	}

	/*
	 * This method might be useful after all:
	 * When a road pops up on another road, the new one has to be removed!
	 */
	public void removeRoad(Line2D road) {
		roadList.remove(road);
		repaint();
	}
	
	
	public void clear() {
		buildingList.clear();
		roadList.clear();
		repaint();
	}
	
	
}